(function (vc) {
    vc.extends({
        data: {
            addShopInfo: {
                shopId: '',
                shopName: '',
                shopLogo: '',
                shopType: '',
                openType: '',
                shopDesc: '',
                sendAddress: '',
                returnPerson: '',
                returnLink: '',
                returnAddress: '',
                accountBondAmount: '',
                mapX: '',
                mapY: '',
                accountBonds: [],
                selectProv: '',
                selectCity: '',
                selectArea: '',
                areaCode: '',
                areas: [],
                provs: [],
                citys: [],
                openTypes: [],
                communitys: [],
            }
        },
        _initMethod: function () {
            vc.getDict('s_shop', "open_type", function (_data) {
                $that.addShopInfo.openTypes = _data;
            });
        },
        _initEvent: function () {
            vc.on('addShop', 'openAddShopModal', function () {
                $that._initArea('101', '0');
                $that.clearAddShopInfo();
                $('#addShopModel').modal('show');
                var map = new AMap.Map("container", {
                    zoom: 14, //初始化地图层级
                    resizeEnable: true
                });
                // 同时引入工具条插件，比例尺插件和鹰眼插件
                AMap.plugin([
                    'AMap.ToolBar',
                    'AMap.Geolocation',
                ], function () {
                    // 在图面添加工具条控件，工具条控件集成了缩放、平移、定位等功能按钮在内的组合控件
                    map.addControl(new AMap.ToolBar());
                    // 在图面添加定位控件，用来获取和展示用户主机所在的经纬度位置
                    map.addControl(new AMap.Geolocation());
                });
                //为地图注册click事件获取鼠标点击出的经纬度坐标
                map.on('click', function (e) {
                    vc.toast("成功获取坐标。");
                    $that.addShopInfo.mapX = e.lnglat.getLng()
                    $that.addShopInfo.mapY = e.lnglat.getLat()
                });
            });
            vc.on("addShop", "notifyUploadCoverImage", function (_param) {
                if (_param.length > 0) {
                    $that.addShopInfo.shopLogo = _param[0].url;
                } else {
                    $that.addShopInfo.shopLogo = '';
                }
            });
        },
        methods: {
            _listShopRanges: function (_page, _rows) {
                let param = {
                    params: {
                        shopTypeId: $that.addShopInfo.shopType,
                        page: _page,
                        row: _rows,
                    }
                };
                //发送get请求
                vc.http.apiGet('/shopRange/queryShopRange',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.addShopInfo.shopRanges = _json.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            addShopValidate() {
                return vc.validate.validate({
                    addShopInfo: $that.addShopInfo
                }, {
                    'addShopInfo.shopName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商铺名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "128",
                            errInfo: "商铺名称太长"
                        },
                    ],
                    'addShopInfo.shopLogo': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商铺logo不能为空"
                        }
                    ],
                    'addShopInfo.shopType': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商铺类型不能为空"
                        },
                        {
                            limit: "num",
                            param: "",
                            errInfo: "商铺类型格式错误"
                        },
                    ],
                    'addShopInfo.openType': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "开店类型不能为空"
                        }
                    ],
                    'addShopInfo.shopDesc': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商铺描述不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "512",
                            errInfo: "商铺描述太长"
                        },
                    ],
                    'addShopInfo.sendAddress': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "发货地址不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "512",
                            errInfo: "发货地址太长"
                        },
                    ],
                    'addShopInfo.returnPerson': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "退货联系人不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "退货联系人太长"
                        },
                    ],
                    'addShopInfo.returnLink': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "退货联系电话不能为空"
                        }
                    ],
                    'addShopInfo.mapX': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "坐标X位置不能为空"
                        }
                    ],
                    'addShopInfo.mapY': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "坐标Y位置不能为空"
                        }
                    ],
                    'addShopInfo.returnAddress': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "退货地址不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "512",
                            errInfo: "退货地址太长"
                        },
                    ],
                });
            },
            saveShopInfo: function () {
                if (!$that.addShopValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                if (!$that.addShopInfo.accountBondAmount) {
                    $that.addShopInfo.accountBondAmount = '';
                    vc.toast("请联系管理员设置该商铺类型的保证金！");
                    return;
                }
                vc.http.apiPost(
                    '/shop.saveShop',
                    JSON.stringify($that.addShopInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addShopModel').modal('hide');
                            $that.clearAddShopInfo();
                            vc.emit('shop', 'listShop', {});
                            vc.toast("开店成功");
                            return;
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            getProv: function (_prov) {
                $that._initArea('202', _prov);
            },
            getCity: function (_city) {
                $that._initArea('303', _city);
            },
            getArea: function (_area) {
                $that.addShopInfo.areaCode = _area;
            },
            _initArea: function (_areaLevel, _parentAreaCode) { //加载区域
                let _param = {
                    params: {
                        areaLevel: _areaLevel,
                        parentAreaCode: _parentAreaCode
                    }
                };
                vc.http.apiGet('/area.listAreas',
                    _param,
                    function (json, res) {
                        if (res.status == 200) {
                            let _tmpAreas = JSON.parse(json).data;
                            if (_areaLevel == '101') {
                                $that.addShopInfo.provs = _tmpAreas;
                            } else if (_areaLevel == '202') {
                                $that.addShopInfo.citys = _tmpAreas;
                            } else {
                                $that.addShopInfo.areas = _tmpAreas;
                            }
                            return;
                        }
                        //$that.$emit('errorInfoEvent',json);
                    }, function (errInfo, error) {
                        console.log('请求失败处理', errInfo, error);
                        vc.toast("查询地区失败");
                    });
            },
            _listAddAccountBonds: function (_shopTypeId) {
                var param = {
                    params: {
                        page: 1,
                        row: 100,
                        bondType: '6006',
                        objId: _shopTypeId
                    }
                };
                //发送get请求
                vc.http.apiGet('/accountBond.queryAccountBond',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.addShopInfo.total = _json.total;
                        $that.addShopInfo.records = _json.records;
                        $that.addShopInfo.accountBonds = _json.data;
                        if (_json.data.length == 0) {
                            vc.toast("请联系管理员设置该商铺类型的保证金！");
                            return;
                        }
                        $that.addShopInfo.accountBondAmount = _json.data[0].amount;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            
            clearAddShopInfo: function () {
                let _openTypes = $that.addShopInfo.openTypes;
                vc.emit('uploadImageUrl', 'clearImage',{});
                
                $that.addShopInfo = {
                    shopName: '',
                    shopLogo: '',
                    shopType: '',
                    openType: '',
                    shopDesc: '',
                    sendAddress: '',
                    returnPerson: '',
                    returnLink: '',
                    returnAddress: '',
                    accountBondAmount: '',
                    accountBonds: [],
                    mapX: '',
                    mapY: '',
                    selectProv: '',
                    selectCity: '',
                    selectArea: '',
                    areaCode: '',
                    areas: [],
                    provs: [],
                    citys: [],
                    openTypes: _openTypes,
                    communitys: [],
                };
            },
            _changeAddShopType: function (_shopTypeId) {
                $that.addShopInfo.shopRangeSel = [];
                $that.addShopInfo.accountBondAmount = '';
                $that._listAddAccountBonds(_shopTypeId);
            },
        }
    });
})(window.vc);
